package com.shipwe.loans.ui.activity

import android.view.View
import com.shipwe.loans.base.app.ShipweStackManager
import com.shipwe.loans.databinding.ActivitySplashBinding
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.ui.presenter.AppSplashPresenter
import com.shipwe.loans.ui.view.IAppSplashView
import com.shipwe.loans.utils.ShipweTitleBarUtil
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.helper.ShipweAppAnalysisHelper
import com.shipwe.loans.utils.helper.TestDataLibInit
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import java.util.TimeZone

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 19:02
 * @Annotation：启动页
 */
class ActivitySplash: ShipweActivity(),IAppSplashView{
    private lateinit var binding: ActivitySplashBinding
    var presenter: AppSplashPresenter? = null

    override fun getShipweRootView(): View {
        binding = ActivitySplashBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initShipweView() {
        ShipweTitleBarUtil.setStatusBarLightMode(this, false)
        isInterceptBack(true)
        //关闭所有页面
        ShipweStackManager.finishAllActivity(ActivitySplash::class.java)
        //启动需要重置审核中、放款中显示的公告提示次数
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_DIALOG_NOTICE_NUM,0)
    }

    override fun initShipweListener() {
        binding.splashAppName.setOnClickListener {
            presenter?.loadAppCommonTimeZoneAction()
        }
    }

    override fun initShipweData() {
        presenter = AppSplashPresenter(this)
        presenter?.loadAppCommonTimeZoneAction()
    }

    /**
     * 时区转发成功
     */
    override fun showSplashTimeZoneRequestData(dataString: String) {
        var timeZone = ""
        var zoneCode = ""
        var zoneCountry = ""

        dataString.split(",").apply {
            if (this.isNotEmpty()){
                timeZone = this[0].trim()
            }
            if (this.size >= 2){
                zoneCode = this[1].trim()
            }
            if (this.size >= 3){
                zoneCountry = this[2].trim()
            }
        }

        //保存时区id
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_TIMEZONE_ID, TimeZone.getDefault().id)
        //保存转发路径
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_TIMEZONE,timeZone)
        //保存转发地区 区号
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_TIMEZONE_CODE,zoneCode)
        //保存国家标识
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_TIMEZONE_COUNTRY,zoneCountry)

        //初始化analysis lib 赋值 转发地区
        ShipweAppAnalysisHelper.initAnalysisLibAtZoneSuccess()
        //初始化 data lib
        TestDataLibInit.initDataLibAtZoneSuccess()
        //添加埋点
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LAUNCH_START)

        presenter?.loadAppCommonConfigsAction()
    }

    /**
     * 进入权限页
     */
    override fun showSplashPermissionPage() {
        onIntent(ActivityPermission::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    override fun showSplashLoginPage() {
        onIntent(ActivityLogin::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    override fun showSplashMainPage() {
        onIntent(ActivityMain::class.java)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LAUNCH_END)
    }
}